﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Text;


namespace News.CYT369.Com.DAL.DbContexts
{
    public partial class NewsDbContext: Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext<News.CYT369.Com.Domains.Identity.SysUser,
                                                                                                            News.CYT369.Com.Domains.Identity.UserType,
                                                                                                            int,
                                                                                                            News.CYT369.Com.Domains.Identity.UserClaim,
                                                                                                            News.CYT369.Com.Domains.Identity.UserRole,
                                                                                                            News.CYT369.Com.Domains.Identity.UserLogin,
                                                                                                            News.CYT369.Com.Domains.Identity.RoleClaim,
                                                                                                            News.CYT369.Com.Domains.Identity.UserToken>
    {
        public NewsDbContext()
        {
        }

        public NewsDbContext(DbContextOptions<NewsDbContext> options)
            : base(options)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (News.CYT369.Com.Domains.SystemSettingService.Instance.DbType == "SQLSERVER2008")
            {
                optionsBuilder.UseSqlServer(News.CYT369.Com.Domains.SystemSettingService.Instance.DefaultConnection).ReplaceService<Microsoft.EntityFrameworkCore.Query.IQueryTranslationPostprocessorFactory, Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServer2008QueryTranslationPostprocessorFactory>();
            }
            else
            {
                optionsBuilder.UseSqlServer(News.CYT369.Com.Domains.SystemSettingService.Instance.DefaultConnection);
            }

            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            #region Documents
            builder.Entity<News.CYT369.Com.Domains.UserDocCategory>(entity => {
                entity.ToTable("TB_UserDocCategory");
                entity.HasOne(c => c.Parent).WithMany(c => c.SubCategorys).HasPrincipalKey(c => c.Id).HasForeignKey(c => c.ParentID);
            });

            builder.Entity<News.CYT369.Com.Domains.Document>(entity => {
                entity.ToTable("TB_Document").HasKey(c => c.Id);
                entity.HasQueryFilter(c=>c.Deleted == false && c.Published == true);
            });

            builder.Entity<News.CYT369.Com.Domains.DocumentExt>(entity =>
            {
                entity.ToTable("TB_DocumentExt");
                entity.HasKey(c=>c.DocID);
                entity.HasOne(c => c.Doc).WithOne(c => c.DocExt).HasForeignKey<News.CYT369.Com.Domains.DocumentExt>(c => c.DocID);
            });

            builder.Entity<News.CYT369.Com.Domains.DocAttachment>(entity =>
            {
                entity.ToTable("TB_DocAttachment");
                entity.HasOne(c => c.Doc).WithMany(c => c.Attachments).HasForeignKey(c => c.DocID);
            });

            builder.Entity<News.CYT369.Com.Domains.DocumentComment>(entity =>
            {
                entity.ToTable("TB_DocumentComment");
            });
            #endregion


            base.OnModelCreating(builder);
        }




    }
}
